﻿using System;
using UnityEngine;
namespace HBFramework
{
    /// <summary>
    /// 秒计时器
    /// </summary>
    public class SecondTimer : ITimer
    {
        private float time;
        private Action<float> onUpdate;
        private Action onStop;
        private Action onFinish;
        private float curTime;
        private bool isStop;

        bool ITimer.IsStop => isStop;

        public SecondTimer(float time, Action<float> onUpdate = null, Action onStop = null, Action onFinish = null)
        {
            this.onUpdate = onUpdate;
            this.onStop = onStop;
            this.time = time;
            this.onFinish = onFinish;
        }

        void ITimer.OnStart()
        {
            curTime = 0;
        }

        void ITimer.OnUpdate()
        {
            curTime += Time.deltaTime;
            onUpdate?.Invoke(curTime);
            if (curTime >= time)
            {
                onFinish?.Invoke();
                isStop = true;
            }
        }

        void ITimer.OnStop()
        {
            onStop?.Invoke();
        }
    }
}
